package my.soft.aiteacher.teacher.mapStruct;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.swagger.v3.core.util.Json;
import my.soft.aiteacher.teacher.pojo.TbLessonPlansDo;
import my.soft.aiteacher.teacher.pojo.TbLessonPlansVo;
import my.soft.aiteacher.teacher.pojo.lessonplan.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

import java.util.Collections;
import java.util.List;

@Mapper(componentModel = "spring")
public interface LessonPlansMapStruct {
    @Mappings({
            @Mapping(target = "basicInfo", source = "basicInfo", qualifiedByName = "StringToBasicInfo"),
            @Mapping(target = "teachingObjectives", source = "teachingObjectives", qualifiedByName = "StringToTeachingObjectives"),
            @Mapping(target = "keyPoints", source = "keyPoints", qualifiedByName = "StringTokeyPoint"),
            @Mapping(target = "difficultPoints", source = "difficultPoints", qualifiedByName = "StringToDifficultPoints"),
            @Mapping(target = "teachingMethods", source = "teachingMethods", qualifiedByName = "StringToTeachingMethod"),
            @Mapping(target = "preparation", source = "preparation", qualifiedByName = "StringToPreparation"),
            @Mapping(target = "process", source = "process", qualifiedByName = "StringToProcess"),
            @Mapping(target = "assignment", source = "assignment", qualifiedByName = "StringToAssignment"),
            @Mapping(target = "blackboardDesign", source = "blackboardDesign", qualifiedByName = "StringToBlackboardDesign"),
            @Mapping(target = "reflection", source = "reflection", qualifiedByName = "StringToReflection")
    })
    TbLessonPlansVo toVo(TbLessonPlansDo lessonPlansDo);

    @Mappings(
            {
                    @Mapping(target = "basicInfo", source = "basicInfo", qualifiedByName = "toJsonString"),
                    @Mapping(target = "teachingObjectives", source = "teachingObjectives", qualifiedByName = "toJsonString"),
                    @Mapping(target = "keyPoints", source = "keyPoints", qualifiedByName = "toJsonString"),
                    @Mapping(target = "difficultPoints", source = "difficultPoints", qualifiedByName = "toJsonString"),
                    @Mapping(target = "teachingMethods", source = "teachingMethods", qualifiedByName = "toJsonString"),
                    @Mapping(target = "preparation", source = "preparation", qualifiedByName = "toJsonString"),
                    @Mapping(target = "process", source = "process", qualifiedByName = "toJsonString"),
                    @Mapping(target = "assignment", source = "assignment", qualifiedByName = "toJsonString"),
                    @Mapping(target = "blackboardDesign", source = "blackboardDesign", qualifiedByName = "toJsonString"),
                    @Mapping(target = "reflection", source = "reflection", qualifiedByName = "toJsonString")
            })
    TbLessonPlansDo toDo(TbLessonPlansVo lessonPlansVo);


    @Named("toJsonString")
    default String toJsonString(Object obj) {
        if (obj == null) {
            return null;
        }
        return JSON.toJSONString(obj,
                SerializerFeature.WriteMapNullValue,  // 序列化null字段
                SerializerFeature.PrettyFormat        // 格式化输出（可选）
        );
    }

    @Named("StringToJson")
    default Object StringToJson(String str, Object o) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return JSON.parseObject(str, o.getClass());
    }

    @Named("StringToDifficultPoints")
    default List<DifficultPoint> StringToDifficultPoints(String str) {
        if (str == null || str.isEmpty()) {
            return Collections.emptyList();
        }
        return JSON.parseArray(str, DifficultPoint.class);
    }

    @Named("StringTokeyPoint")
    default List<KeyPoint> StringTokeyPoint(String str) {
        if (str == null || str.isEmpty()) {
            return Collections.emptyList();
        }
        return JSON.parseArray(str, KeyPoint.class);
    }

    @Named("StringToTeachingMethod")
    default List<TeachingMethod> StringToTeachingMethod(String str) {
        if (str == null || str.isEmpty()) {
            return Collections.emptyList();
        }
        return JSON.parseArray(str, TeachingMethod.class);
    }

    @Named("StringToReflection")
    default List<Reflection> StringToReflection(String str) {
        if (str == null || str.isEmpty()) {
            return Collections.emptyList();
        }
        return JSON.parseArray(str, Reflection.class);
    }

    @Named("StringToBasicInfo")
    default BasicInfo StringToBasicInfo(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return JSON.parseObject(str, BasicInfo.class);
    }

    @Named("StringToTeachingObjectives")
    default TeachingObjectives StringToTeachingObjectives(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return JSON.parseObject(str, TeachingObjectives.class);
    }

    @Named("StringToPreparation")
    default Preparation StringToPreparation(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return JSON.parseObject(str, Preparation.class);
    }

    @Named("StringToProcess")
    default TeachingProcedure StringToProcess(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return JSON.parseObject(str, TeachingProcedure.class);
    }

    @Named("StringToAssignment")
    default Assignment StringToAssignment(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return JSON.parseObject(str, Assignment.class);
    }

    @Named("StringToBlackboardDesign")
    default BlackboardDesign StringToBlackboardDesign(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return JSON.parseObject(str, BlackboardDesign.class);
    }

}


